package com.service.employeeservice.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mapper.EmployeeDutyMapper;
import com.mapper.EmployeeMapper;
import com.mapper.PrivilegeMapper;
import com.mapper.RolePrivilegeMapper;
import com.pojo.Employee;
import com.pojo.EmployeeDuty;
import com.pojo.EmployeeDutyExample;
import com.pojo.EmployeeExample;
import com.pojo.EmployeeExample.Criteria;
import com.pojo.Privilege;
import com.pojo.RolePrivilege;
import com.pojo.RolePrivilegeExample;
import com.service.employeeservice.EmployeeService;

@Service
public class EmployeeServiceImpl implements EmployeeService{
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeDutyMapper employeeDutyMapper;
	@Autowired
	private RolePrivilegeMapper rolePrivilegeMapper;
	@Autowired
	private PrivilegeMapper privilegeMapper;
	
	@Override
	public Employee login(Employee employee) {
		if (employee!=null) {
			EmployeeExample example = new EmployeeExample();
			Criteria criteria = example.createCriteria();
			criteria.andEmployeeusernameEqualTo(employee.getEmployeeusername());
			criteria.andEmployeepasswordEqualTo(employee.getEmployeepassword());
			List<Employee> list = employeeMapper.selectByExample(example);
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public List<Privilege> getPrivilege(Integer employeeid) {
		if (employeeid!=null) {
			//根据用户id查询用户对应的角色对象
			EmployeeDutyExample employeeDutyExample = new EmployeeDutyExample();
			com.pojo.EmployeeDutyExample.Criteria createCriteria = employeeDutyExample.createCriteria();
			createCriteria.andEmployeeidEqualTo(employeeid);
			EmployeeDuty employeeDuty = employeeDutyMapper.selectByPrimaryKey(employeeid);
			//根据用户角色对象里的角色id查询用户权限
			RolePrivilegeExample rolePrivilegeExample = new RolePrivilegeExample();
			com.pojo.RolePrivilegeExample.Criteria createCriteria2 = rolePrivilegeExample.createCriteria();
			createCriteria2.andDutyidEqualTo(employeeDuty.getDutyid());
			List<RolePrivilege> list = rolePrivilegeMapper.selectByExample(rolePrivilegeExample);
			
			List<Privilege> privileges = new ArrayList<Privilege>();
		    //根据用户的权限查询用户对应的菜单栏	
			for (RolePrivilege rolePrivilege : list) {
				privileges.add(privilegeMapper.selectByPrimaryKey(rolePrivilege.getPkPrivilege()));
			}
			return privileges;
		}
		return null;
	}

}
